<?php
class Friend extends CI_Controller {
	
	/**
	 * @return CI_Controller
	 */
	public static function get_instance() {
		
	}
	
	function index() {
		if($this->session->userdata('is_logged_in'))
		{
			if(isset($_GET['id'])){
				$user_id=$_GET['id'];
				$this->load->model('user_model');
				$this->load->model('user_setting_model');
				$this->load->model('city_model');
				$this->load->model('friends_model');
				
				$is_friend=$this->friends_model->is_friend($user_id);
				
				$user=$this->user_model->get_user_info($user_id);
				$user_setting=$this->user_setting_model->get_user_setting_info($user_id);
				$city=$this->city_model->get_city_info($user->city_id);
				
				//friend_name
				$friend_name = strlen($user_setting->n_name)!=0? $user_setting->n_name:$user->user_name;
				
				$data=array(
					'user'=>$user,
					'user_setting'=>$user_setting,
					'city'=>$city,	
					'is_friend'=>$is_friend,
					'friend_name'=>$friend_name					
				);
				$this->load->view('friend/main',$data);
			}
		}
		else
			redirect(base_url());
	}
	
	function add_friend(){
		if($this->session->userdata('is_logged_in'))
		{
			$fuid=$_POST['fuid'];
			$note=$_POST['note'];
			$this->load->model('friends_model');
			$this->load->model('friendlog_model');
			
			$result1=$this->friends_model->add_friend($fuid);
			$result2=$this->friendlog_model->add_friendlog($fuid,$note);
			
			if($result1 && $result2){
				echo '请求已发送，等待对方验证中';	
			}
			else{
				echo '添加失败';
			}
			
		}
		else
			redirect(base_url());
	}
	
}
?>